<?php

namespace App\Encore\Admin\Grid\Displayers;

use App\Encore\Admin\Admin;

class RowSelector extends AbstractDisplayer
{
    public function display()
    {
        Admin::script($this->script());

        return <<<EOT
<input type="checkbox" lay-skin="primary"  lay-filter="{$this->grid->getGridRowName()}" class="{$this->grid->getGridRowName()}-checkbox" data-id="{$this->getKey()}" />
EOT;
    }

    protected function script()
    {
        $allName = $this->grid->getSelectAllName();
        $rowName = $this->grid->getGridRowName();

        $selected = trans('admin.grid_items_selected');

        return <<<EOT
var form = layui.form
form.on('checkbox($allName)', function(data){
    console.log(data.elem.checked);
    $('.{$rowName}-checkbox').prop("checked",data.elem.checked);
    form.render('checkbox');
});
EOT;
    }
}
